﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DDD.Core;
using FloatComparer = DDDTest.Helper.FloatComparer;

namespace DDDTest.Core {
    [TestClass]
    public class TestVertexBuffer {
        [TestMethod]
        public void Test_New1 () {
            var vbuf = new VertexBuffer ();

            Assert.AreEqual (0, vbuf.VertexArrayCount);
            Assert.AreEqual (0, vbuf.VertexCount);
        }

        [TestMethod]
        public void Test_AddVertexArray () {
            var vbuf = new VertexBuffer ();
            var varry = new VertexArray<float> (4, 3);

            vbuf.AddVertexArray ("Charlotte E Yeager", varry);
            Assert.AreEqual (1, vbuf.VertexArrayCount);
            Assert.AreEqual (4, vbuf.VertexCount);

            var attr = vbuf.GetAttribute ("Charlotte E Yeager");
            Assert.AreEqual ("Charlotte E Yeager", attr.Name);
            Assert.AreEqual (varry, attr.VertexArray);
        }

        [TestMethod]
        public void Test_RemoveVertexArray () {
            var vbuf = new VertexBuffer ();
            var varry = new VertexArray<float> (4, 3);

            vbuf.AddVertexArray ("Charlotte E Yeager", varry);
            vbuf.RemoveVertexArray (varry);

            Assert.AreEqual (0, vbuf.VertexArrayCount);
            Assert.AreEqual (0, vbuf.VertexCount);
        }

        [TestMethod]
        public void Test_Indexer () {
            var vbuf = new VertexBuffer ();
            var varry = new VertexArray<float> (4, 3);
            vbuf.AddVertexArray ("Sakamoto Mio", varry);

            Assert.AreEqual (varry, vbuf["Sakamoto Mio"]);
        }


        [TestMethod]
        public void Test_IEnumerator () {
            var vbuf = new VertexBuffer ();
            var eila = new VertexArray<float> (4, 1);
            var sanya = new VertexArray<float> (4, 2);
            var trude = new VertexArray<float> (4, 4);

            vbuf.AddVertexArray ("Sanya V.Litvyak", sanya);
            vbuf.AddVertexArray ("Eila Ilmatar Juutilainen", eila);
            vbuf.AddVertexArray ("Gertrud Barkhorn", trude);

            var sum1 = vbuf.Sum (x => x.VertexArray.ComponentCount);
            var sum2 = vbuf.Attributes.Sum (x => x.VertexArray.ComponentCount);

            Assert.AreEqual (7, sum1);
            Assert.AreEqual (7, sum2);
        }

        [TestMethod]
        public void Test_References () {
            var vary1 = new VertexArray<float> (4, 1);
            var vary2 = new VertexArray<float> (4, 2);
            var vary3 = new VertexArray<float> (4, 4);

            var vbuf = new VertexBuffer ();
            vbuf.AddVertexArray ("VertexArray 1", vary1);
            vbuf.AddVertexArray ("VertexArray 2", vary2);
            vbuf.AddVertexArray ("VertexArray 3", vary3);

            var objs = vbuf.References;

            Assert.AreEqual (3, objs.Count ());
            Assert.AreEqual (true, objs.All (x => x != null));
        }

        [TestMethod]
        public void Test_Find () {
            var vary1 = new VertexArray<float> (4, 1);
            var vary2 = new VertexArray<float> (4, 2);
            var vary3 = new VertexArray<float> (4, 4);

            var vbuf = new VertexBuffer ();
            vbuf.AddVertexArray ("VertexArray 1", vary1);
            vbuf.AddVertexArray ("VertexArray 2", vary2);
            vbuf.AddVertexArray ("VertexArray 3", vary3);

            vary1.UserID = 101;
            vary2.UserID = 102;
            vary3.UserID = 103;
            vbuf.UserID = 104;

            Assert.AreEqual (vary1, vbuf.Find (101));
            Assert.AreEqual (vary2, vbuf.Find (102));
            Assert.AreEqual (vary3, vbuf.Find (103));
            Assert.AreEqual (vbuf, vbuf.Find (104));
        }
    }
}
